<template>
  <div class="searchpage">
    <herder></herder>
    <div>
      <!-- <span>搜索页面</span> -->
      <pagebottom></pagebottom>
      <van-search
        v-model="value"
        placeholder="请输入搜索关键词"
        @input="searchGoods"
      />
    </div>
    <div class="showsearch" v-if="value == '' ? false : true">
      <ul>
        <li
          class="search-li"
          v-for="(item, index) in result"
          @click="setgoodid(item.goodsid)"
        >
          <div class="search-l">
            <span>{{ index + 1 }}.</span>
            <img :src="item.goodscover" alt="" />
          </div>
          <div class="search-r">
            <span>{{ item.goodsname }}</span>
          </div>
        </li>
      </ul>
    </div>
    <div class="history" v-if="value == '' ? true : false">
      <div class="history-title">
        <span>历史搜索</span>
      </div>
      <div class="history-content">
        <span
          v-if="value == '' ? true : false"
          v-for="(item, index) in shistory"
          @click="value=item"
          >{{ item }}</span
        >
      </div>
    </div>
    <div class="hotsearch" v-if="value == '' ? true : false">
      <ul>
        <li @click="value = '牙刷'">
          <img src="../assets/images/banner/banner-img.jpg" alt="" />
          <span >牙刷</span>
        </li>
          <li @click="value = '口罩'">
          <img src="../assets/images/banner/banner-img2.jpg" alt="" />
          <span @click="value = '口罩'">口罩</span>
        </li>
          <li @click="value = '清洁器'">
          <img src="../assets/images/banner/pro-banner-bg.jpg" alt="" />
          <span @click="value = '清洁器'">清洁器</span>
        </li @click="value = '分析仪'">
          <li>
          <img src="../assets/images/banner/sidebar-banner.png" alt="" />
          <span @click="value = '分析仪'">牙刷</span>
        </li>
      </ul>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.hotsearch {
  ul {
      display: flex;
      flex-wrap: wrap;
    width: 375px;
    padding: 10px 10px;
    li {
      width: 50%;
      position: relative;
      padding: 10px;
      span {
        position: absolute;
        left: 18px;
        top: 18px;
        color: #fafafa;
        font-size: 14px;
        font-weight: 800;
      }
      img {
        width: 100%;
        height: 130px;
      }
    }
  }
}
.searchpage {
  //   margin-top: 100px;
  // top: 100px;
}
.history {
  width: 375px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: left;
  text-align: left;
  padding: 10px 10px;

  .history-content {
    display: flex;
    justify-content: left;
    align-items: center;
    flex-wrap: wrap;
    margin-top: 10px;
    span {
      margin-left: 10px;
    }
  }
  .history-title {
    span {
      font-size: 16px;
    }
  }
  span {
    // display: block;
    padding: 6px 6px;
    height: 30px;
    width: auto;
    border-radius: 10px;
    font-size: 12px;
    color: #777;
    background-color: #fafafa;
  }
}
.search-li {
  border-bottom: 1px solid #fafafa;
  height: 60px;
  display: flex;
  width: 300px;
  margin: 0 auto;
  justify-content: start;
  .search-l {
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      margin-right: 10px;
      font-size: 10px;
      color: #777;
    }
    img {
      width: 20px;
      height: 20px;
    }
  }
  .search-r {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-left: 10px;
    span {
      font-size: 10px;
      color: #777;
    }
  }
}
.searchpage {
  margin-top: 40px;
}
</style>
<script>
import herder from "../components/herder.vue";
import pageBottom from "../components/pagebottom";
import Pagebottom from "../components/pagebottom";
import "../http/request";
import { goodssearchFunc } from "../http/request";
import { mapState } from "vuex";
let d = null;
export default {
  components: {
    herder,
    pageBottom,
    Pagebottom,
  },
  data() {
    return {
      result: {},
      value: "",
      shistory: [],
    };
  },
  watch: {
    value() {
      if (this.value == "") {
        this.result = {};
      } else {
        console.log("watch");
        this.searchGoods();
      }
    },
  },
  computed: {},
  methods: {
    searchGoods() {
      if (d != null && value == "") return;

      d = setTimeout(() => {
        goodssearchFunc("/goods/search", { keyword: this.value }).then(
          (data) => {
            console.log("data", data);
            this.result = data;
            d = null;
          }
        );
      }, 500);
    },
    setgoodid(value) {
      console.log("历史数组", this.shistory);
      this.shistory.push(`${this.value}`);
      localStorage.setItem("history", JSON.stringify(this.shistory));
      console.log(value);
      this.$router.push("productdetail");
      localStorage.setItem("goodsid", value);
    },
  },
  created() {
    this.$store.commit("setactive", 1);
    this.shistory = JSON.parse(localStorage.getItem("history"));
    this.shistory=[...new Set(this.shistory)]
    console.log("历史搜索数组", this.shistory);
  },
};
</script>
